﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using CustomsSystem.BLL;
using CustomsSystem.Model;
using Maticsoft.Common;
using Microsoft.Win32;

using Word = Microsoft.Office.Interop.Word;

namespace WEB.T_document
{
    public partial class Up_document : System.Web.UI.Page
    {

        CodeManage code = new CodeManage();
        DocumentManage docMan = new DocumentManage();
        document doc = new document();
        SheetInfoManage SM = new SheetInfoManage();
        DataSet ds = new DataSet();
        CustomsSystem.BLL.info_document DM = new CustomsSystem.BLL.info_document();
        CustomsSystem.BLL.info_documentMastert BLDM = new CustomsSystem.BLL.info_documentMastert();
        PageRecord pr1 = new PageRecord();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != "")
                {
                    int sheetID = Convert.ToInt32(Request.QueryString["ID"]);
                    Label4.Text = sheetID.ToString();//记录sheetID
                    //Label4.Visible = false;//sheetID不显示
                    //Label3.Text = string.Empty;

                    DDLdocType.DataSource = BLDM.GetAllList();//绑定模板下拉框
                    DDLdocType.DataTextField = "docViewName";
                    DDLdocType.DataValueField = "docViewID";
                    DDLdocType.DataBind();
                   
                    if (DDLdocType.SelectedValue != "")//如果没有模板，不能录入数据
                    {
                        if (docMan.Exists(" sheetID=" + Convert.ToInt32(Label4.Text)))
                        {
                            ds = docMan.GetList("sheetID=" + Label4.Text.Trim());//取得单证表的数据
                            DDLdocType.SelectedIndex = DDLdocType.Items.IndexOf(DDLdocType.Items.FindByValue(ds.Tables[0].Rows[0]["docType"].ToString()));//绑定已经选择的模板的名称
                            DDLdocType.Enabled = false;//不能修改
                            #region 可上传单证的数量(需修改)
                            if (ds.Tables[0].Rows[0]["docName4"].ToString().Trim() != "")
                            {
                                MessageBox.ShowAndRedirect(this, "", "");
                                return;
                            }
                            else if (ds.Tables[0].Rows[0]["docName3"].ToString().Trim() != "")
                            {
                                lb_docNum.Text = "您还可以上传1次单证";
                            }
                            else if (ds.Tables[0].Rows[0]["docName2"].ToString().Trim() != "")
                            {
                                lb_docNum.Text = "您还可以上传2次单证";
                            }
                            else if (ds.Tables[0].Rows[0]["docName1"].ToString().Trim() == "")
                            {
                                lb_docNum.Text = "您还可以上传4次单证";
                            }
                            else
                            {
                                lb_docNum.Text = "您还可以上传3次单证";
                            }
                            lb_docNum.Visible = true;
                            #endregion
                        }
                        GridView1.DataSource = DM.GetList("docViewID=" + DDLdocType.SelectedValue);//绑定单证类型的gv
                        GridView1.DataBind();
                    }
                    else
                    {
                        MessageBox.ShowAndRedirect(this, "没有单证模板，请联系管理人", "/workflow/workflowView.aspx");
                    }
                    //label14为 委托单单号
                    //判断是否已录入检查checkbox
                    //bool a = SM.Exists(Convert.ToInt32(Label4.Text));


                }
            }
        }
        /// <summary>
        /// 行数据绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)//判断单证上传checkbox
            {
                if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    int i = ds.Tables[0].Rows[0]["remark"].ToString().IndexOf(e.Row.Cells[0].Text);//如果该单证类型已经上传，即i大于-1
                    if (i > -1)//如果〉-1就是已经上传的单证
                    {
                        e.Row.BackColor = System.Drawing.Color.GreenYellow;//行的颜色变为绿色
                        CheckBox cbox = e.Row.Cells[1].FindControl("cbDoco") as CheckBox;//强制转换checkbox,cell[1]为第二个
                        cbox.Checked = true;//如果判断框已被选中
                        cbox.Enabled = false;//不能再次选择
                    }
                }
            }

        }

        /// <summary>
        /// 上传事件按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bt_UP_Click(object sender, EventArgs e)
        {
         
            string sc = "";//上传单证
            string sc2 = "";//未上传单证
            //读取表的checkbox，循环上传单证
            for (int i = 0; i <= GridView1.Rows.Count - 1; i++)//上传单证
            {
                CheckBox cb = (CheckBox)GridView1.Rows[i].FindControl("cbDoco");
                if (cb != null && cb.Checked == true)
                {
                    sc = sc + GridView1.Rows[i].Cells[0].Text.Trim() + ",";

                }
            }
            //循环未上传单证的
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                //  cb2 == null 
                CheckBox cb2 = (CheckBox)GridView1.Rows[i].FindControl("cbDoco");
                if (cb2.Checked == false)
                {
                    sc2 = sc2 + GridView1.Rows[i].Cells[0].Text.Trim() + ",";
                }
            }

            #region
            string Upfile = WJFileUp.FileName;//上传文件名
            string msg = "";

            // 判断文件有没有输入
            if (WJFileUp.HasFile == true) //判断有没有文件
            {
                if (sc != "")
                {
                    //if (Label4.Text != "")//判断有没有填写委托单号
                    //{
                    int check = Convert.ToInt32(Label4.Text);
                    //bool a = SM.Exists(check);//判断是否传进

                    //录入数据
                    if (!DDLdocType.Enabled)
                    {
                        doc = docMan.GetModel(check);
                    }
                   //string localPath = Server.MapPath("~/document/T_document/"+doc.sheetID+"/");
                    doc.sheetID = Convert.ToInt32(Label4.Text);
                    doc.docType = DDLdocType.SelectedValue;
                    doc.inputDate = Convert.ToDateTime(DateTime.Now);
                    doc.inputEmpNo = Session["Empno"].ToString() ;//录入者的姓名
                    doc.remark = sc.TrimEnd(",".ToCharArray());//将单证类型赋值给remark
                    doc.docName5 = sc2.TrimEnd(",".ToCharArray());//将未上传单证类型赋值给docname5
                    string localPath = Server.MapPath("~/document/T_document/" + doc.sheetID + "/");
                    if (!System.IO.Directory.Exists(localPath))
                    {
                        System.IO.Directory.CreateDirectory(localPath);
                    }

                    //string strWhere = "codeType='docType'";
                    //DataSet DDL = new CustomsSystem.BLL.CodeManage().GetList(strWhere);
                    //DDLdocType.DataSource = BLDM.GetAllList();
                    //DDLdocType.DataTextField = "docViewName";
                    //DDLdocType.DataValueField = "docViewID";
                    //DDLdocType.DataBind();
                    //string KO = DDLdocType.Text;
                    string name1 = "";//文件上传的名字
                    if (doc.docName1 == null)
                    {
                        name1 = doc.sheetID.ToString() + "#1#" + WJFileUp.FileName; 
                        doc.docName1 = name1;
                    }
                    else if (doc.docName2==null)
                    {
                        name1 = doc.sheetID.ToString() + "#2#" + WJFileUp.FileName;
                        doc.docName2 = name1;
                    }
                    else if (doc.docName3 == null)
                    {
                        name1 = doc.sheetID.ToString() + "#3#" + WJFileUp.FileName;
                        doc.docName3 = name1;
                    }
                    else
                    {
                        name1 = doc.sheetID.ToString() + "#4#" + WJFileUp.FileName;
                        doc.docName4 = name1;
                    }

                    //上传文件的名称格式 委托编号#1#文件名
                    string fileleixing = System.IO.Path.GetExtension(WJFileUp.FileName).ToString(); //获取文件的扩展名

                    //int sheetID = Convert.ToInt32(Request.QueryString["ID"]);
                    // doc = docMan.GetModel(sheetID);
                    //if (doc.docName2 != "")
                    //{
                    //    doc.docName2 = "";
                    //    if (doc.docName3 != "")
                    //    {
                    //        doc.docName3 = "";
                    //        if (doc.docName4 != "")
                    //        {
                    //            doc.docName4 = "";
                    //        }
                    //    }
                    //    else
                    //    {
                    //        Response.Write("<script>alert('输入超过四次，请不要重复输入!')</script>");
                    //    }

                    //}
                    //docMan.Add(doc);
                    #region  上传文件
                    //上传文件和判断文件是否超出规定范围的值
                    int filebig = WJFileUp.PostedFile.ContentLength / (1024 * 1024);
                   // string localPath = Server.MapPath("~/document/T_document/") + doc.sheetID.ToString()+"/";//保存路径
                    //if (WJFileUp.HasFile == true)
                    //{
                  
                    if (fileleixing == ".rar" || fileleixing == ".pdf" || fileleixing == ".jpg")//判断格式
                    {
                        if (filebig <= 150)//判断大小
                        {
                            //string name = "'" + name1 + "'";
                            string name =  name1 ;
                            string vsfilename = name1;//获取文件的名称
                            int index = vsfilename.LastIndexOf(".");
                            string vstype = vsfilename.Substring(index).ToLower();//取文件的扩展名
                            string vsnewname = System.DateTime.Now.ToString("yyyyMMddHHmmssffff");//声称文件名，防止重复
                            vsnewname = vsnewname + vstype;//完整的上传文件名
                            string vsurl = localPath+ vsnewname;

                            this.WJFileUp.PostedFile.SaveAs(vsurl);
                        }
                        else
                        {
                            Response.Write("<script>alert('上传文件大小超过了150M!')</script>");
                            return;
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('上传文件类型不对!')</script>");
                        return;
                    }
                    //}
                    #endregion
                    if (DDLdocType.Enabled)
                    {
                        docMan.Add(doc);
                        msg = "已上传文件有" + sc;
                        //GridView1.DataSource = DM.GetList("docViewID=" + DDLdocType.SelectedValue);
                        //GridView1.DataBind();
                    }
                    else
                    {
                        docMan.Update(doc);
                        msg = "已上传文件有" + sc;
                    }

                    //else
                    //{
                    //    Response.Write("<script>alert('没有此委托单号,请认真核查填写!')</script>");
                    //    return;
                    //}
                    //}
                    //else
                    //{
                    //    Response.Write("<script>alert('请填写委托单号!')</script>");
                    //    return;
                    //}
                }
                else
                {
                    Response.Write("<script>alert('请选择上传的单证!')</script>");
                    return;
                }
            }
            else
            {
                msg = "未选择上传文件!";
            }
            MessageBox.ShowAndRedirect(this, msg, "/T_document/Index_document.aspx");
            return;
        }
            #endregion

        //protected void Button1_Click(object sender, EventArgs e)
        //{
        //    ListBox1.Items.Clear();
        //    if (Label4.Text != "")
        //    {
        //        int check = Convert.ToInt32(Label4.Text);
        //        bool a = SM.Exists(check);
        //        if (a == true)
        //        {
        //            Label3.Text = "已经上传的单证有:";
        //            ListBox1.DataSource = docMan.GetList("sheetID=" + Label4.Text.Trim());
        //            ListBox1.DataTextField = "remark";
        //            ListBox1.DataValueField = "remark";
        //            ListBox1.DataBind();
        //        }
        //        else
        //        {
        //            Response.Write("<script>alert('没有此委托单号,请认真核查填写!')</script>");
        //            return;
        //        }
        //    }
        //    else
        //    {
        //        Response.Write("<script>alert('请填写委托单号!')</script>");
        //        return;
        //    }
        //}

        protected void DDLdocType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DDLdocType.SelectedValue != "")
            {
                GridView1.DataSource = DM.GetList("docViewID=" + DDLdocType.SelectedValue);
                GridView1.DataBind();

            }
            else
            {
                Response.Write("<script>alert('没有单证模板，请添加')</script>");
                return;
            }
        }

    }
}
